<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <home-page></home-page>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
  <script>
    // 定义一个第三方实例
    const bus= new Vue();
    const vm = new Vue();
    const CommonTitle = {
      template: `
        <div>
          <h2>子组件1</h2>  
          <button @click="emit">向兄弟组件通信</button>
        </div>
      `,
      data() {
        return {
          msg: '子组件1数据'
        }
      },
      methods: {
        emit() {
          bus.$emit('biubiu', this.msg)
        }
      },
    }
    const CommonTitle2 = {
      template: `
        <div>
          <h2>子组件2</h2>  
          {{ msg }}
        </div>
      `,
      data() {
        return {
          msg: ''
        }
      },
      mounted () {
        bus.$on('biubiu', (msg) =>{
          console.log('我收到了兄弟', msg);
          this.msg = msg;
        })
        
      }
    }
    const HomePage = {
      template: `
        <div>
          <h1>首页</h1>  
          <common-title></common-title>
          <hr/>
          <common-title2></common-title2>
        </div>
      `,
      components: {
        CommonTitle,
        CommonTitle2
      }
    }
    Vue.component('HomePage', HomePage)
    vm.$mount('#app')
  </script>
</body>

</html>